import time

import redis



class AiboxCache(object):

    # cache = Cache(1)
    pool = redis.ConnectionPool(host='10.2.111.66', port=6379, db=2,decode_responses=True)   # host是redis主机，需要redis服务端和客户端都起着 redis默认端口是6379
    cache = redis.Redis(connection_pool=pool)

    def __tt(self):
        print("oo")
    def __init__(self,t):
        # self.cache = Cache(ttl=t)
        # self.cache.ttl = t
        pass

    def testadd(self, x):
        print("无缓存")
        return x

    def get_data(self, x):
        t = self.cache.get(x)
        # if self.cache.has(x):
        if t is not None:
            print("有缓存")
            re = self.cache.get(x)
            return re
        else:
            re = self.testadd(x)
            self.cache.set(x,re,ex=10)
            return x

    def test(self):
        print(self.get_data("select"))
        time.sleep(2)
        # self.cache.clear()
        print(self.get_data("select"))
        time.sleep(2)
        print(self.get_data("select"))

    def pr(self):
        print(self.cache.ttl)



if __name__ == '__main__':
    # pool = redis.ConnectionPool(host='172.17.150.57', port=6379, db=15,password='ehl1234')  # host是redis主机，需要redis服务端和客户端都起着 redis默认端口是6379
    pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=15,password=None)  # host是redis主机，需要redis服务端和客户端都起着 redis默认端口是6379
    cache = redis.Redis(connection_pool=pool)
    # cache.set('a','a')
    # cache.set('11','1')
    # cache.set('2','2')
    # cache.set('3','3')
    print(cache.get('1').decode())
    print(cache.delete('bb'))
    # cache.delete(*cache.keys())
    print('ok')
